package com.realgodo.site.web.user.vote;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.application.vote.Vote;
import com.realgodo.site.entity.application.vote.VoteItem;
import com.realgodo.site.service.application.VoteManager;
import com.realgodo.site.web.user.UserDefaultController;

/**
 * 用户投票功能
 * 
 * @author bijiqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-13下午04:30:23
 * @since jdk1.5
 * @version 0.1
 */
@Scope("prototype")
@Results( {
		@Result(name = "tomy", type = "redirect", location = "${base}/user/vote/index!my"),
		@Result(name = "toshow", type = "redirect", location = "${base}/user/vote/index!show?id=${id}") })
public class IndexController extends UserDefaultController {

	private VoteManager voteManager;

	/** for save and view */
	private Vote vote;
	/** for save and view */
	private List<VoteItem> items;
	/** for view */
	private List<Vote> votes;

	/** VoteItem.id组 */
	private Long[] ids;

	private Page<Vote> votePage = new Page<Vote>(PAGE_SIZE);

	private static final long serialVersionUID = -7282102003279611331L;

	/**
	 * 热门投票
	 */
	public String execute() throws Exception {
		votePage = this.voteManager.getHotVotes(votePage);
		return SUCCESS;
	}

	/**
	 * 初始化 新增投票页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		return "add";
	}

	/**
	 * 朋友的投票
	 * 
	 * @return
	 * @throws Exception
	 */
	public String friends() throws Exception {
		return "friends";
	}

	/**
	 * 我发起的投票
	 * 
	 * @return
	 * @throws Exception
	 */
	public String my() throws Exception {
		votePage = this.voteManager.getMyVotes(votePage, super.getSpace());
		return "my";
	}

	/**
	 * 获取我参与的投票
	 * @return
	 */
	public String join(){
		votePage = this.voteManager.getMyJoinVotes(votePage, super.getSpace());
		return "join";
	}
	/**
	 * 持久化 投票
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		if (null != vote) {
			vote.setSpace(super.getSpace());
			this.voteManager.addVote(vote, items);
		}
		return "tomy";
	}

	/**
	 * 投票详细
	 * 
	 * @return
	 */
	public String show() {
		if (null != getId()) {
			vote = this.voteManager.get(getId());
			items = this.voteManager.getVoteItems(getId());
		}
		return "show";
	}

	/**
	 * 投票
	 * 
	 * @return
	 */
	public String vote() {
		if (null != getId() && ids.length != 0) {
			vote = this.voteManager.get(getId());
			this.voteManager.vote(vote, ids, super.getSpace());
		}
		return "toshow";
	}

	public Vote getVote() {
		return vote;
	}

	public void setVote(Vote vote) {
		this.vote = vote;
	}

	public List<VoteItem> getItems() {
		return items;
	}

	public void setItems(List<VoteItem> items) {
		this.items = items;
	}

	@Autowired
	public void setVoteManager(VoteManager voteManager) {
		this.voteManager = voteManager;
	}

	public List<Vote> getVotes() {
		return votes;
	}

	public void setVotes(List<Vote> votes) {
		this.votes = votes;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Page<Vote> getVotePage() {
		return votePage;
	}

	public void setVotePage(Page<Vote> votePage) {
		this.votePage = votePage;
	}

}
